<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/5/3
 * Time: 22:12
 */




class DingDianTest extends PHPUnit_Framework_TestCase
{

    public function additionProvider()
    {
        return array(
            'host' => 'www.23wx.com',
            'url' => array(
                'search'    => "http://so.23wx.com/cse/search?q={keyword}&click=1&entry=1&s=15772447660171623812",
                'book'      => "http://www.23wx.com/book/57820",
                'chapter'   => "http://www.23wx.com/html/{bookPrefix}/{bookId}/",
                'content'   => "http://www.23wx.com/html/{bookPrefix}/{bookId}/{chapterId}.html",
                'category'  => "http://www.23wx.com",
                'bookList'  => "http://www.23wx.com/class/{category}_{page}.html",
            ),
            'rule' => array(
                'category' => array(
                    'main' => array('start' => 'm_menu', 'end' => 'main'),
                    're'   => '/<a href\="(?<categoryUrl>.*?)">(?<categoryName>.*?)<\/a>/',
                ),
                'bookList' => array(
                    'main' => array('start' => 'content', 'end' => 'pages'),
                    're'   => '/<tr bgcolor\="#FFFFFF">([\w\W]*?)<td class\="L"><a href\="http:\/\/www\.23wx\.com\/book\/(?<bookId>[\d]+)">(?<bookName>.*?)<\/a><\/td>([\w\W]*?)<td class\="L"><a href\="http:\/\/www\.23wx\.com\/html\/(?<bookPrefix>[\d]+?)\/([\d]+)\/" target\="_blank">(.*?)<\/a><\/td>([\w\W]*?)<td class\="C">(?<bookAuthor>.*?)<\/td>([\w\W]*?)<td class\="R">(?<bookWordsNumber>.*?)<\/td>([\w\W]*?)<td class\="C">(?<bookLastUpdate>.*?)<\/td>([\w\W]*?)<td class\="C">(?<bookState>.*?)<\/td>([\w\W]*?)<\/tr>/',
                ),
                'chapter' => array(
                    'main' => array('start' => 'id="at"', 'end' => 'id="foottips"'),
                    're'   => '/<a href\="(?<chapterId>[\d]+)\.html">(?<chapterName>.*?)<\/a>/',
                ),
                'content' => array(
                    'main' => array(),
                    're'   => '/contents">(?<content>.*?)<\/dd>/',
                ),
                'search'  => array(
                    'main' => array('start' => 'result-list', 'end' => 'extra'),
                    're'   => '/<a cpos\="img" href\="(?<bookUrl>http:\/\/www.23wx.com\/html\/(.*?)\/)"([\w\W]*?)<img src\="(?<bookThumb>http:\/\/www.23wx.com\/files\/article\/image\/(.*?).jpg)"([\w\W]*?)<a cpos\="title"([\w\W]*?)title\="(?<bookName>.*?)"([\w\W]*?)<p class\="result-game-item-desc">(?<bookDesc>.*?)<\/p>/',
                )
            )
        );
    }

    public function testBase()
    {
        $config = $this->additionProvider();
        $object = new BookSpider\Provider\DingDian($config);
        return $object;
    }

    public function testExpectBarActualBaz()
    {
        $this->expectOutputString('bar');
        print 'bar';
    }

    /**
     * @depends testBase
     */
    public function testCategory($object)
    {
        $result = $object->category();
        $this->assertArrayHasKey('categoryUrl', $result, 'failed: categoryUrl');
        $this->assertArrayHasKey('categoryName', $result, 'failed: categoryName');
        $this->assertCount(12, $result['categoryUrl']);
    }

    /**
     * @depends testBase
     */
    public function testBookList($object)
    {
        $result = $object->bookList(1,3);
        $this->assertArrayHasKey('bookId', $result, 'failed: categoryUrl');
        $this->assertArrayHasKey('bookName', $result, 'failed: categoryName');
        //$this->assertCount(12, $result['categoryUrl']);
    }

    /**
     * @depends testBase
     */
    public function testChapter($object)
    {
        $object->chapter(52647);
    }
}